package com.ly.thread;

import java.util.concurrent.TimeUnit;

class Source {

    private String lockA = "lockA";

    private String lockB = "lockB";


    public void lockA() throws InterruptedException {
        synchronized(lockA){
            System.out.println(Thread.currentThread().getName() +lockB);
            TimeUnit.SECONDS.sleep(2);
            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() +lockA);
            }
        }
    }

    public void lockB() throws InterruptedException {
        synchronized(lockB){
            System.out.println(Thread.currentThread().getName() +lockA);
            TimeUnit.SECONDS.sleep(2);
            synchronized (lockA) {
                System.out.println(Thread.currentThread().getName() +lockB);
            }
        }
    }

}

public class DeadLockDemo {

    public static void main(String[] args) {
        Source source = new Source();
        new Thread( () -> {
            try {

                source.lockA();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();
        new Thread( () -> {
            try {
                source.lockB();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }
}
